package com.hdwmw.json;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hdwmw.entity.GiftEntity;
import com.hdwmw.entity.Goods;
import com.hdwmw.entity.GoodsCate;
import com.hdwmw.entity.MerchanInfo;
import com.hdwmw.entity.UserExistResult;
import com.hdwmw.entity.UserInfo;

public class JsonParser {

	public static UserExistResult getUserExist(String json) {
		UserExistResult uer = new UserExistResult();
		try {
			JSONObject js = new JSONObject(json);
			String RESP_DESC = js.optString("RESP_DESC");
			String RESP_CODE = js.optString("RESP_CODE");
			uer.setRESP_DESC(RESP_DESC);
			uer.setRESP_CODE(RESP_CODE);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return uer;
	}
	
	public static List<MerchanInfo> getMerchan(String json) {
		List<MerchanInfo> list_merchan = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("RESP_CODE");
			if(status.equals("1")) {
				list_merchan = new ArrayList<MerchanInfo>();
				JSONArray ja = jo.optJSONArray("QRY_SHOP_INFO_RSP_DATA");
				if(ja != null && ja.length() > 0) {
					for(int i = 0; i < ja.length(); i++) {
						JSONObject jo_merchan = ja.optJSONObject(i);
						MerchanInfo merchanInfo = new MerchanInfo();
						merchanInfo.setBeginTime(jo_merchan.optString("begintime"));
						merchanInfo.setEndTime(jo_merchan.optString("endtime"));
						merchanInfo.setPre1(jo_merchan.optString("pre1"));
						merchanInfo.setPre2(jo_merchan.optString("pre2"));
						merchanInfo.setShopAddress(jo_merchan.optString("address"));
						merchanInfo.setShopID(jo_merchan.optString("shopid"));
						merchanInfo.setShopImgUrl(jo_merchan.optString("imgUrl"));
						merchanInfo.setShopName(jo_merchan.optString("shopname"));
						merchanInfo.setShopNotice(jo_merchan.optString("notice"));
						merchanInfo.setShopPhone(jo_merchan.optString("phone"));
						merchanInfo.setStatus(jo_merchan.optString("status"));
						merchanInfo.setUserid(jo_merchan.optString("userid"));
						merchanInfo.setTuijian(jo_merchan.optString("tuijian"));
						list_merchan.add(merchanInfo);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list_merchan;
	}
	
	public static List<Goods> getGoods(String json) {
		List<Goods> list_goods = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("RESP_CODE");
			if(status.equals("1")) {
				list_goods = new ArrayList<Goods>();
				JSONArray ja = jo.optJSONArray("QRY_GOODS_INFO_DATA");
				if(ja != null && !ja.equals("null") && ja.length() > 0) {
					for(int i = 0; i < ja.length(); i++) {
						Goods goods = new Goods();
						JSONObject jo_goods = ja.getJSONObject(i);
						goods.setGoodsales(jo_goods.optString("sales"));
						goods.setGoodsCategoryid(jo_goods.optString("categoryid"));
						goods.setGoodsid(jo_goods.optString("goodsid"));
						goods.setGoodsimgUrl(jo_goods.optString("imgUrl"));
						goods.setGoodsName(jo_goods.optString("goodsname"));
						goods.setGoodspre1(jo_goods.optString("pre1"));
						goods.setGoodspre2(jo_goods.optString("pre2"));
						goods.setGoodsPrice(jo_goods.optString("price"));
						goods.setGoodsShopid(jo_goods.optString("shopid"));
						goods.setGoodstimes(jo_goods.optString("times"));
						list_goods.add(goods);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list_goods;
	}
	
	public static List<GoodsCate> getGoodsCategory(String json) {
		List<GoodsCate> list_goodsCategory = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("RESP_CODE");
			if(status.equals("1")) {
				list_goodsCategory = new ArrayList<GoodsCate>();
				JSONArray ja = jo.getJSONArray("QRY_GOODS_CATEGORY_RSP_DATA");
				if(ja != null && !ja.equals("null") && ja.length() > 0) {
					for(int i = 0; i < ja.length(); i++) {
						JSONObject cate_jo = ja.getJSONObject(i);
						GoodsCate category = new GoodsCate();
						category.setCategoryid(cate_jo.optString("categoryid"));
						category.setCategoryname(cate_jo.optString("categoryname"));
						list_goodsCategory.add(category);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list_goodsCategory;
	}
	
	public static UserInfo getUser(String json) {
		UserInfo userInfo = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("RESP_CODE");
			if(status.equals("1")) {
				JSONObject ja = jo.getJSONObject("USER_INFO");
						userInfo = new UserInfo();
						userInfo.setAddress(ja.optString("address"));
						userInfo.setJifen(ja.optString("jifen"));
						userInfo.setNumber(ja.optString("number"));
						userInfo.setPre1(ja.optString("pre1"));
						userInfo.setUserid(ja.optString("userid"));
						userInfo.setUserName(ja.optString("username"));
						userInfo.setPassword(ja.optString("password"));
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return userInfo;
	}
	
	public static List<GiftEntity> getGift(String json) {
		List<GiftEntity> list_gift = null;
		try {
			JSONObject jo = new JSONObject(json);
			String status = jo.optString("RESP_CODE");
			if(status.equals("1")) {
				list_gift = new ArrayList<GiftEntity>();
				JSONArray ja = jo.getJSONArray("QRY_PRESENT_RESP_DATA");
				if(ja != null && !ja.equals("null") && ja.length() > 0) {
					for(int i = 0; i < ja.length(); i++) {
						JSONObject gift_jo = ja.getJSONObject(i);
						GiftEntity gift = new GiftEntity();
						gift.setGiftID(gift_jo.optString("presentid"));
						gift.setGiftName(gift_jo.optString("presentname"));
						gift.setGift_img_url(gift_jo.optString("imgUrl"));
						gift.setGift_sy(gift_jo.optString("amount"));
						gift.setGift_need_jf(gift_jo.optString("jifen"));
						list_gift.add(gift);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return list_gift;
	}
}
